<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回对象的类名</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.get-class" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">get_class</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">get_class</span> &mdash; <span class="dc-title">返回对象的类名</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.get-class-description">
  <h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
   <span class="methodname"><strong>get_class</strong></span>
    ([ <span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   返回对象实例 <code class="parameter">object</code> 所属类的名字。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.get-class-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">object</code></dt>

     <dd>

      <p class="para">
       要测试的对象。如果在类里，此参数可以省略。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.get-class-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回对象实例 <code class="parameter">object</code>
   所属类的名字。
   如果 <code class="parameter">object</code> 不是一个对象则返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   如果在一个类里，省略了参数 <code class="parameter">object</code>，
   则返回当前所在类的名称。
  </p>
  <p class="para">
    如果 <code class="parameter">object</code> 是命名空间中某个类的实例，则会返回带上命名空间的类名。
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.get-class-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果用其他类型调用 <span class="function"><strong>get_class()</strong></span>，而不是一个对象的话，就会产生 <strong><code>E_WARNING</code></strong> 级别的错误。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.get-class-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0 起</td>
       <td>
        <code class="parameter">object</code> 默认参数现在是 <strong><code>NULL</code></strong> ，所以，现在传入 <strong><code>NULL</code></strong> 到 <code class="parameter">object</code> 参数时，和没传参数的结果一样。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.get-class-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="function.usort.examples.object">
    <p><strong>Example #1 使用 <span class="function"><strong>get_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"My&nbsp;name&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;an&nbsp;object<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;external&nbsp;call<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Its&nbsp;name&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;internal&nbsp;call<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Its name is foo
My name is foo
</pre></div>
    </div>
   </div>
   <div class="example" id="function.usort.examples.closure">
    <p><strong>Example #2 超类中使用 <span class="function"><strong>get_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">abstract&nbsp;class&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">{<br />}<br /><br />new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
</pre></div>
    </div>
   </div>
   <div class="example" id="example-6139">
    <p><strong>Example #3 命名空间中的类使用 <span class="function"><strong>get_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">Foo</span><span style="color: #007700">\</span><span style="color: #0000BB">Bar</span><span style="color: #007700">;<br /><br />class&nbsp;</span><span style="color: #0000BB">Baz&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Foo</span><span style="color: #007700">\</span><span style="color: #0000BB">Bar</span><span style="color: #007700">\</span><span style="color: #0000BB">Baz</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$baz</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(11) &quot;Foo\Bar\Baz&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.get-class-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="get_called_class.html" class="function" rel="rdfs-seeAlso">get_called_class()</a> - 后期静态绑定（&quot;Late Static Binding&quot;）类的名称</span></li>
    <li class="member"><span class="function"><a href="get_parent_class.html" class="function" rel="rdfs-seeAlso">get_parent_class()</a> - 返回对象或类的父类名</span></li>
    <li class="member"><span class="function"><a href="gettype.html" class="function" rel="rdfs-seeAlso">gettype()</a> - 获取变量的类型</span></li>
    <li class="member"><span class="function"><a href="is_subclass_of.html" class="function" rel="rdfs-seeAlso">is_subclass_of()</a> - 如果此对象是该类的子类，则返回 TRUE</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>